package quanlyhochieu.nopnhanhc;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import utils.DateUtils;
import dbutil.ConnectDB;

public class NopNhanHC {
	private String error;
	private String ten;
	private String thoiGian;
	private List<NguoiNopNhan> listPerson;
	
	public NopNhanHC(){}
	public String getError() {
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public String getTen() {
		return ten;
	}
	public void setTen(String ten) {
		this.ten = ten;
	}
	public String getThoiGian() {
		return thoiGian;
	}
	public void setThoiGian(String thoiGian) {
		this.thoiGian = thoiGian;
	}
	public List<NguoiNopNhan> getListPerson() {
		return listPerson;
	}
	public void setListPerson(List<NguoiNopNhan> listPerson) {
		this.listPerson = listPerson;
	}
	public java.sql.Date layNgayTroVeTruoc6Thang(java.util.Date date){
		java.util.Date current = new java.util.Date();
		int day = DateUtils.LayNgayHienTai();
		int month = DateUtils.LayThangHienTai();
		int year = DateUtils.LayNamHienTai();
		String strDate = "";
		if(month > 6){
			month -= 6;
			strDate =year+"-"+month+"-"+day;
		}
		else {
			year-= 1;
			month = 6 - month;
			strDate = year+"-"+month+"-"+day;
			
		}
		return java.sql.Date.valueOf(strDate);
	}
	public List<Infor> getListInfor(){
		List<Infor> result = new ArrayList<Infor>();
		java.util.Date current = new java.util.Date();
		String ACC = "";
		if(this.thoiGian.equals("month")){
			java.sql.Date dateBeforSixMonth = this.layNgayTroVeTruoc6Thang(current);			
			ACC = " and NK.ngaythaotac > '"+dateBeforSixMonth+"' and NK.ngaythaotac < '"+DateUtils.utilDateToSqlDate(current)+"'";
		}
		else{
			int year = DateUtils.LayNamHienTai();
			int month = DateUtils.LayThangHienTai();
			int day = DateUtils.LayNgayHienTai();
			
			year = year-1;
			String date = year+"-"+month+"-"+day;
			java.sql.Date dateBeforSixMonth = java.sql.Date.valueOf(date);
			ACC = " and NK.ngaythaotac > '"+dateBeforSixMonth+"' and NK.ngaythaotac < '"+DateUtils.utilDateToSqlDate(current)+"'";
		}
		String sql = "select * from canhan as CN, hochieu as HC, nhatkytranhanhc as NK where CN.cmnd = HC.cmnd and NK.sohc = HC.sohc and CN.ten like '%"+this.ten+"%' "+ACC;
		System.out.println(sql);
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(sql);
		System.out.println(array.size());
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();		
			for (int i = 0; i < array.size(); i++) {
				hash = (HashMap<String, Object>) array.get(i);
				Infor infor = new Infor();
				infor.setCmnd(hash.get("cmnd").toString());
				infor.setCoQuan(hash.get("coquan").toString());
				infor.setNgayThaoTac(DateUtils.formatDate(hash.get("ngaythaotac").toString()));
				infor.setSoHC(hash.get("sohc").toString());
				infor.setTen(hash.get("ten").toString());
				String thaoTac = hash.get("thaotac").toString();
				if(thaoTac.equals("G")) infor.setThaoTac("Giao");
				else infor.setThaoTac("Nhận");
				System.out.println("Ten "+infor.getTen()+" soHC "+infor.getSoHC());
				result.add(infor);
			}
		}
		return result;
	}
	
	public List<Infor> LayDSInforGiongCMND(List<Infor> list, String cmnd){
		List<Infor> result = new ArrayList<Infor>();
		for (int i =0;i<list.size();i++) {
			Infor infor = list.get(i);
			if(infor.getCmnd().equals(cmnd)){
				result.add(infor);
				System.out.println("Sublist "+infor.getTen());
			}
		}
		list.removeAll(result);
		return result;
	}
	public boolean checkValue(){
		boolean result = false;
		if(this.ten == null || this.ten.equals("") ||
			this.thoiGian == null || this.thoiGian.equals("")){
			this.setError("Bạn phải điền đầy đủ thông tin có dấu (*)");
			result = false;
		}
		else {
			this.setError("");
			result = true;
		}
		return result;
	}
	
	int stt  =0;	
	public String thongKe(){
		if(this.checkValue()){
		List<NguoiNopNhan> list = new ArrayList<NguoiNopNhan>();
		List<Infor> listInfor = this.getListInfor();
		List<Infor> tempList = this.getListInfor();
		for (Infor infor : listInfor) {
			List<HoChieuNopNhan> listHC = new ArrayList<HoChieuNopNhan>();
			List<Infor> subList = this.LayDSInforGiongCMND(tempList, infor.getCmnd());
			System.out.println("Size sub list "+subList.size());
			if(subList.size() > 0){
				stt+=1;
				NguoiNopNhan nguoiNopNhan = new NguoiNopNhan();
				String thongTin = infor.getTen()+" CMND: "+infor.getCmnd()+" Cơ Quan: "+infor.getCoQuan();
				nguoiNopNhan.setTenNguoi(thongTin);
				nguoiNopNhan.setCmnd(infor.getCmnd());
				nguoiNopNhan.setCoQuan(infor.getCoQuan());
				nguoiNopNhan.setStt(stt);
				int subStt  =0;
			for (Infor infor2 : subList) {
				subStt+=1;
				HoChieuNopNhan HC = new HoChieuNopNhan();
				HC.setStt(subStt);
				HC.setNgayThaoTac(infor2.getNgayThaoTac());
				HC.setSoHC(infor2.getSoHC());
				HC.setThaoTac(infor2.getThaoTac());	
				listHC.add(HC);
				
			}
			nguoiNopNhan.setListNopNhan(listHC);
			nguoiNopNhan.setTongNopNhan(listHC.size());
			list.add(nguoiNopNhan);		
			}
		}
		System.out.println("size list person "+list.size());
		this.setError("");
		this.setListPerson(list);
		}
		return "nopnhanhc";
	}
	public String reset(){
		this.listPerson = null;
		this.thoiGian = null;
		this.ten = null;
		this.setError("");
		return "nopnhanhc";
	}

}
